package com.bdqn;

import java.lang.reflect.Field;

public class Sj02 {
    public static void main(String[] args) {
        try {
            Class clz = Class.forName("com.bdqn.Person");
            try {
                Object person = clz.newInstance();
                try {
                    Field name = clz.getDeclaredField("name");
                    name.setAccessible(true);
                    System.out.println("赋值前的Name"+name.get(person));
                    //为name赋值
                    name.set(person,"New Person");
                    System.out.println("赋值后的name"+name.get(person));
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
